package com.sgb.kjwl.view.ui.adapter.self.packers;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemSelfAddVideoBinding;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.io.File;
import java.util.List;

public class SelfTeamVideoAdapter extends BaseRecycleAdapter<String> {
    private static final String TAG_ADD = "add";

    private int mMaxSize;

    public SelfTeamVideoAdapter(Context context, List<String> datas, int maxSize) {
        super(context, R.layout.item_self_add_video, datas);
        mMaxSize = maxSize;
        if (getDatas().size() < mMaxSize) {
            getDatas().add(TAG_ADD);
        }
    }

    @Override
    public int getItemCount() {
        return getDatas().size();
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.getmBinding().getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position >= getRealCount()) {
                    if (onItemClick != null) {
                        onItemClick.onItemClick("", position);
                    }
                } else {
                    if (onItemClick != null) {
                        onItemClick.onItemClick(datas.get(position), position);
                    }
                }
            }
        });
        if (getRealCount() == mMaxSize && position == mMaxSize) {
            ((ItemSelfAddVideoBinding) holder.getmBinding()).llAdd.setVisibility(View.GONE);
        } else {
            ((ItemSelfAddVideoBinding) holder.getmBinding()).llAdd.setVisibility(View.VISIBLE);
        }
        try {
            convert(holder, datas.get(position));
        } catch (IndexOutOfBoundsException e) {
        }
    }

    @SuppressLint("CheckResult")
    @Override
    public void convert(ItemViewHolder holder, final String entity) {
        holder.setBinding(BR.viewModel, entity);
        if (!TextUtils.equals(entity, TAG_ADD)) {
            ((ItemSelfAddVideoBinding) holder.getmBinding()).rlDelete.setTag(holder.getAdapterPosition());
            ((ItemSelfAddVideoBinding) holder.getmBinding()).rlDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getRealCount() == mMaxSize) {
                        getDatas().remove(holder.getAdapterPosition());
                        getDatas().add(TAG_ADD);
                    } else {
                        getDatas().remove(holder.getAdapterPosition());
                    }
                    notifyDataSetChanged();
                }
            });
            ((ItemSelfAddVideoBinding) holder.getmBinding()).rlDelete.setVisibility(View.VISIBLE);
            ((ItemSelfAddVideoBinding) holder.getmBinding()).ivPicturePlay.setVisibility(View.VISIBLE);
            if (!entity.startsWith("http")) {
                Glide.with(context)
                        .asBitmap()
                        .load(Uri.fromFile(new File(entity)))
                        .apply(new RequestOptions().placeholder(R.drawable.null_img_list))
                        .into(((ItemSelfAddVideoBinding) holder.getmBinding()).ivPicture);
            } else {
                Glide.with(context)
                        .asBitmap()
                        .load(entity)
                        .apply(new RequestOptions().placeholder(R.drawable.null_img_list))
                        .into(((ItemSelfAddVideoBinding) holder.getmBinding()).ivPicture);
            }
            ((ItemSelfAddVideoBinding) holder.getmBinding()).ivAdd.setVisibility(View.GONE);

        } else {
            ((ItemSelfAddVideoBinding) holder.getmBinding()).ivAdd.setVisibility(View.VISIBLE);
            ((ItemSelfAddVideoBinding) holder.getmBinding()).rlDelete.setVisibility(View.GONE);
            ((ItemSelfAddVideoBinding) holder.getmBinding()).ivPicturePlay.setVisibility(View.GONE);
            ((ItemSelfAddVideoBinding) holder.getmBinding()).ivPicture.setImageResource(0);
        }
    }

    public int getRealCount() {
        for (String data : getDatas()) {
            if (TAG_ADD.equals(data)) {
                return getDatas().size() - 1;
            }
        }
        return getDatas().size();
    }

    public int hasCount() {
        return mMaxSize - getRealCount();
    }

    public void setPicData(List<LocalMedia> mediaList) {
        if (mediaList != null && mediaList.size() > 0) {
            for (LocalMedia localMedia : mediaList) {
                getDatas().add(0, localMedia.getRealPath());
            }
            if (getRealCount() == mMaxSize) {
                getDatas().remove(TAG_ADD);
            }
            notifyDataSetChanged();
        }
    }

    public void setData(List<String> datas) {
        if (datas != null && datas.size() > 0) {
            getDatas().addAll(0, datas);
            if (getRealCount() == mMaxSize) {
                getDatas().remove(TAG_ADD);
            }
        } else {
            getDatas().clear();
            getDatas().add(TAG_ADD);
        }
        notifyDataSetChanged();
    }
}
